package com.zly.framework.rbac.handler.failed;

import com.zly.framework.rbac.exception.SystemException;
import com.zly.framework.rbac.model.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class ExceptionRestControllerHandler {
    @ExceptionHandler(Exception.class)
    public Result<Object> exception(Exception e) {
        log.error(e.getMessage(), e);
        return Result.ERROR("系统错误");
    }

    @ExceptionHandler(SystemException.class)
    public Result<Object> sysException(SystemException e) {
        return Result.ERROR(e.getMessage());

    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<Object> validateException(MethodArgumentNotValidException e) {
        StringBuilder sb = new StringBuilder();
        for (ObjectError allError : e.getAllErrors()) {
            sb.append(" ").append(allError.getDefaultMessage());
        }
        return Result.ERROR(sb.toString());
    }
}
